package ch.elektroplan.mueller.passwordSave.presentation;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Scanner;

import ch.elektroplan.mueller.passwordSave.R;
import ch.elektroplan.mueller.passwordSave.business.PasswordSafe;
import ch.elektroplan.mueller.passwordSave.business.supporters.Enigma;
import ch.elektroplan.mueller.passwordSave.business.supporters.Password_DB_Generator;
import ch.elektroplan.mueller.passwordSave.presentation.onClicklisteners.ShowPasswordListener;
import android.app.Activity;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;

public class Unused extends Activity {

	private PasswordSafe passwordSafe;
	private LinearLayout linearLayout;
	private Button button;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.passwordlist);

		button = (Button) findViewById(R.id.bu_passwordlist_filldb);
		button.setOnClickListener(new onfillDbclick());
		passwordSafe = new PasswordSafe(this);

		linearLayout = (LinearLayout) findViewById(R.id.ll_pwlist);

		for (int i = 0; i < passwordSafe.getNamesArr().length; i++) {

			Button button = new Button(this);
			button.setText(passwordSafe.getNamesArr()[i]);

			button.setOnClickListener(new ShowPasswordListener(this,
					passwordSafe.getWordsArr()[i]));

			linearLayout.addView(button);

		}
	}

	private class onfillDbclick implements OnClickListener {

		File namefile;
		File firstnamefile;
		File ipfile;
		File passwordfile;
		File usernamefile;
		File typefile;

		String[] typearr;
		String[] namearr;
		String[] firstnamearr;
		String[] userarr;
		String[] iparr;
		String[] passwordarr;

		public void onClick(View v) {
			try {
				namefile = new File("/sdcard/passwordsafe/name.txt");
				firstnamefile = new File("/sdcard/passwordsafe/firstname.txt");
				ipfile = new File("/sdcard/passwordsafe/ip.txt");
				passwordfile = new File("/sdcard/passwordsafe/password.txt");
				usernamefile = new File("/sdcard/passwordsafe/username.txt");
				//typefile = new File("/sdcard/passwordsafe/type.txt");
			} catch (Exception e) {
				Toast.makeText(Unused.this, "fail: filesetup",
						Toast.LENGTH_SHORT).show();
			}

			try {
				namearr = makearr(namefile);
				firstnamearr = makearr(firstnamefile);
				userarr = makearr(usernamefile);
				iparr = makearr(ipfile);
				passwordarr = makearr(passwordfile);
			//	typearr = makearr(typefile);

			} catch (Exception e) {
				Toast.makeText(Unused.this, "fail: makearr",
						Toast.LENGTH_SHORT).show();
			}

			Password_DB_Generator generator = new Password_DB_Generator(
					Unused.this);
			SQLiteDatabase db = generator.getWritableDatabase();
			ContentValues cv = new ContentValues();
			Enigma enigma = new Enigma();

			try {

				for (int i = 0; i < namearr.length; i++) {

					cv.put(Password_DB_Generator.NAME, enigma.encode(namearr[i]));
					cv.put(Password_DB_Generator.FIRSTNAME, enigma.encode(firstnamearr[i]));
					cv.put(Password_DB_Generator.USERNAME, enigma.encode(userarr[i]));
					cv.put(Password_DB_Generator.IP, enigma.encode(iparr[i]));
					cv.put(Password_DB_Generator.PASSWORD, enigma.encode(passwordarr[i]));
					//cv.put(Password_DB_Generator.TYPE, enigma.encode(typearr[i]));
					
					db.insert(Password_DB_Generator.USERTABLE, null, cv);
				}
				
				
			} catch (Exception e) {
				Toast.makeText(Unused.this, "fail: fill db",
						Toast.LENGTH_SHORT).show();
			}
			
			Toast.makeText(Unused.this, "finished",Toast.LENGTH_SHORT).show();
		}

		private String[] makearr(File f) throws FileNotFoundException {

			FileReader fileReader = null;
			ArrayList<String> arrlist = new ArrayList<String>();

			fileReader = new FileReader(f);

			Scanner scanner = new Scanner(fileReader);

			while (scanner.hasNext()) {
				String input = scanner.nextLine();
				arrlist.add(input);
			}

			String[] temp = new String[arrlist.size()];
			for (int i = 0; i < temp.length; i++) {
				temp[i] = arrlist.get(i);
			}
			return temp;

		}

	}
}
